import { c, random } from '../../utils/arrays'
import { pad } from '../../utils/filters'

export function stringify (bet) {
  var [[q, w], [e, r]] = bet.split('|').map(s => s.split('*').map(b => b.split(',')))
  return `<b>${w ? `(${q.join(' ')}) ${w.join(' ')}` : q.join(' ')}</b>
          <a>${r ? `(${e.join(' ')}) ${r.join(' ')}` : e.join(' ')}</a>`
}

export function betContent (cart) {
  return cart.map(c => {
    var [q, w, e, r] = c.groups
    var bet
    if (c.bold) {
      bet = q.map(pad).join(',') + '*' + w.map(pad).join(',') + '|'
      if (e && e.length) {
        bet += e.map(pad).join(',') + '*'
      }
      bet += r.map(pad).join(',')
      return bet
    } else {
      bet = q.map(pad).join(',') + '|' + w.map(pad).join(',')
    }
    return bet
  }).join(';')
}

/**
 * 大乐透 注数计算
 * @param groups 号码分组二维数组
 * @param bold   是否胆拖玩法
 * @returns {number}
 */
export function bets (groups, bold) {
  var [q, w = [], e = [], r = []] = groups

  // 胆拖时
  if (bold) {
    if (q && w.length > 1 && r.length > 1) {
      return c(w.length, 5 - q.length) * c(r.length, 2 - e.length)
    }
  }
  // 非胆拖时
  else {
    if (q && w) {
      return c(q.length, 5) * c(w.length, 2)
    }
  }
  return 0
}

/**
 * 生成投注
 */
export function gen () {
  return [
    random(1, 35, 5),
    random(1, 12, 2)
  ]
}
